package com.x.controller;

import com.x.pojo.Schedule;
import com.x.service.ScheduleService;
import com.x.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: ScheduleController
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.09.15 17:05
 */

@CrossOrigin //允许其他源访问后台项目的controller
@RestController
@ComponentScan("com.x")
@RequestMapping("schedule")
@Slf4j
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;


    @GetMapping("/{pageSize}/{currentPage}")
    public R page(@PathVariable int pageSize,
                  @PathVariable int currentPage){

        R r = scheduleService.page(pageSize, currentPage);
        //slf4j日志输出
        log.info("ScheduleController返回的数据为: {}", r);
        return r;
    }

    @DeleteMapping("/{id}")
    public R remove(@PathVariable Integer id){

        R r = scheduleService.remove(id);

        return r;
    }

    @PostMapping
    public R save(@Validated @RequestBody Schedule schedule, BindingResult result){ //@RequestBody用来解析json格式数据

        if(result.hasErrors()){
            return R.fail(null);
        }else{
            R r = scheduleService.save(schedule);
            return r;
        }
    }


    @PutMapping
    public R update(@Validated @RequestBody Schedule schedule, BindingResult result){

        if(result.hasErrors()){
            return R.fail(null);
        }
        R r = scheduleService.update(schedule);
        return r;

    }
}
